package com.cc.allreturn;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.springframework.http.HttpStatus;

import java.util.HashMap;
import java.util.Map;

/**
 * @User c.c
 * @Since 2023/5/12
 * @Time 11:01
 * @Description
 */

@AllArgsConstructor
@NoArgsConstructor
@Data
@ToString
@EqualsAndHashCode
public class AjaxReturnBean<T> {
  private int code;
  private String msg;
  private T data;
  private long count;

  public static <T> AjaxReturnBean<T> createSuccess() {
    return new AjaxReturnBean(AjaxReturnBean.Code.SUCCESS.valueOf(), "", (Object) null, 0L);
  }

  public static <T> AjaxReturnBean<T> createSuccess(T data) {
    return new AjaxReturnBean(AjaxReturnBean.Code.SUCCESS.valueOf(), "SUCCESS", data, 0L);
  }

  public static <T> AjaxReturnBean<T> createSuccess(String msg) {
    return new AjaxReturnBean(AjaxReturnBean.Code.SUCCESS.valueOf(), msg, (Object) null, 0L);
  }

  public static <T> AjaxReturnBean<T> createSuccess(String msg, T data) {
    return new AjaxReturnBean(AjaxReturnBean.Code.SUCCESS.valueOf(), msg, data, 0L);
  }

  public static <T> AjaxReturnBean<T> createSuccess(String msg, T data, long count) {
    return new AjaxReturnBean(AjaxReturnBean.Code.SUCCESS.valueOf(), msg, data, count);
  }

  public static <T> AjaxReturnBean<T> createError(String msg) {
    return new AjaxReturnBean(AjaxReturnBean.Code.ERROR.valueOf(), msg, (Object) null, 0L);
  }

  public static <T> AjaxReturnBean<T> createError(Code code, String msg) {
    return new AjaxReturnBean(code.valueOf(), msg, (Object) null, 0L);
  }

  public static <T> AjaxReturnBean<T> createError(HttpStatus code, String msg) {
    return new AjaxReturnBean(code.value(), msg, (Object) null, 0L);
  }

  public static <T> AjaxReturnBean<T> createError(int code, String msg) {
    return new AjaxReturnBean(code, msg, (Object) null, 0L);
  }

  public static <T> AjaxReturnBean<T> createError(String msg, T data) {
    return new AjaxReturnBean(AjaxReturnBean.Code.ERROR.valueOf(), msg, data, 0L);
  }

  public boolean success() {
    return AjaxReturnBean.Code.SUCCESS.valueOf() == this.code;
  }

  public Map<String, Object> toMap() {
    Map<String, Object> map = new HashMap(4);
    map.put("code", this.code);
    map.put("msg", this.msg);
    map.put("data", this.data);
    map.put("count", this.count);
    return map;
  }


  public static enum Code {
    SUCCESS(0),
    ERROR(-1);

    private final int value;

    private Code(int value) {
      this.value = value;
    }

    public int valueOf() {
      return this.value;
    }
  }
}
